//
// File: interp1.cpp
//
// MATLAB Coder version            : 5.4
// C/C++ source code generated on  : 23-Apr-2025 16:26:18
//

// Include Files
#include "interp1.h"
#include "ppval.h"
#include "rt_nonfinite.h"
#include "spline.h"
#include "coder_array.h"
#include "omp.h"
#include <cmath>
#include <string.h>

// Type Definitions
struct b_struct_T {
  coder::array<double, 2U> breaks;
  coder::array<double, 2U> coefs;
};

// Function Declarations
namespace coder {
static void interp1SplineOrPCHIP(const ::coder::array<double, 1U> &y,
                                 const ::coder::array<double, 2U> &xi,
                                 ::coder::array<double, 2U> &yi,
                                 const ::coder::array<double, 1U> &x);

}

// Function Definitions
//
// Arguments    : const ::coder::array<double, 1U> &y
//                const ::coder::array<double, 2U> &xi
//                ::coder::array<double, 2U> &yi
//                const ::coder::array<double, 1U> &x
// Return Type  : void
//
namespace coder {
static void interp1SplineOrPCHIP(const ::coder::array<double, 1U> &y,
                                 const ::coder::array<double, 2U> &xi,
                                 ::coder::array<double, 2U> &yi,
                                 const ::coder::array<double, 1U> &x)
{
  array<double, 2U> b_y;
  b_struct_T pp;
  double d;
  int loop_ub;
  b_y.set_size(1, y.size(0));
  loop_ub = y.size(0);
  for (int i{0}; i < loop_ub; i++) {
    b_y[i] = y[i];
  }
  spline(x, b_y, pp.breaks, pp.coefs);
  loop_ub = xi.size(1) - 1;
#pragma omp parallel for num_threads(omp_get_max_threads()) private(d)

  for (int k = 0; k <= loop_ub; k++) {
    d = xi[k];
    if (std::isnan(d)) {
      yi[k] = rtNaN;
    } else {
      yi[k] = ppval(pp.breaks, pp.coefs, d);
    }
  }
}

//
// Arguments    : const ::coder::array<double, 1U> &varargin_1
//                const ::coder::array<double, 1U> &varargin_2
//                const ::coder::array<double, 2U> &varargin_3
//                ::coder::array<double, 2U> &Vq
// Return Type  : void
//
void interp1(const ::coder::array<double, 1U> &varargin_1,
             const ::coder::array<double, 1U> &varargin_2,
             const ::coder::array<double, 2U> &varargin_3,
             ::coder::array<double, 2U> &Vq)
{
  array<double, 1U> x;
  array<double, 1U> y;
  int k;
  int n;
  int nx;
  boolean_T b;
  y.set_size(varargin_2.size(0));
  n = varargin_2.size(0);
  for (k = 0; k < n; k++) {
    y[k] = varargin_2[k];
  }
  x.set_size(varargin_1.size(0));
  n = varargin_1.size(0);
  for (k = 0; k < n; k++) {
    x[k] = varargin_1[k];
  }
  nx = varargin_1.size(0) - 1;
  Vq.set_size(1, varargin_3.size(1));
  n = varargin_3.size(1);
  for (k = 0; k < n; k++) {
    Vq[k] = 0.0;
  }
  b = (varargin_3.size(1) == 0);
  if (!b) {
    k = 0;
    int exitg1;
    do {
      exitg1 = 0;
      if (k <= nx) {
        if (std::isnan(varargin_1[k])) {
          exitg1 = 1;
        } else {
          k++;
        }
      } else {
        if (varargin_1[1] < varargin_1[0]) {
          double xtmp;
          int b_j1;
          k = (nx + 1) >> 1;
          for (b_j1 = 0; b_j1 < k; b_j1++) {
            xtmp = x[b_j1];
            n = nx - b_j1;
            x[b_j1] = x[n];
            x[n] = xtmp;
          }
          if (varargin_2.size(0) > 1) {
            n = varargin_2.size(0) - 1;
            nx = varargin_2.size(0) >> 1;
            for (k = 0; k < nx; k++) {
              xtmp = y[k];
              b_j1 = n - k;
              y[k] = y[b_j1];
              y[b_j1] = xtmp;
            }
          }
        }
        interp1SplineOrPCHIP(y, varargin_3, Vq, x);
        exitg1 = 1;
      }
    } while (exitg1 == 0);
  }
}

//
// Arguments    : const ::coder::array<double, 1U> &y
//                const ::coder::array<double, 1U> &xi
//                ::coder::array<double, 1U> &yi
//                const ::coder::array<double, 1U> &x
// Return Type  : void
//
void interp1SplineOrPCHIP(const ::coder::array<double, 1U> &y,
                          const ::coder::array<double, 1U> &xi,
                          ::coder::array<double, 1U> &yi,
                          const ::coder::array<double, 1U> &x)
{
  array<double, 2U> b_y;
  b_struct_T pp;
  int loop_ub;
  b_y.set_size(1, y.size(0));
  loop_ub = y.size(0);
  for (int i{0}; i < loop_ub; i++) {
    b_y[i] = y[i];
  }
  spline(x, b_y, pp.breaks, pp.coefs);
  loop_ub = xi.size(0) - 1;
#pragma omp parallel for num_threads(omp_get_max_threads())

  for (int k = 0; k <= loop_ub; k++) {
    if (std::isnan(xi[k])) {
      yi[k] = rtNaN;
    } else {
      yi[k] = ppval(pp.breaks, pp.coefs, xi[k]);
    }
  }
}

} // namespace coder

//
// File trailer for interp1.cpp
//
// [EOF]
//
